package com.example.mypopupwindow;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void leoclick(View view){
        View popupview=getLayoutInflater().inflate(R.layout.popup_view,null);
        Button btn1=popupview.findViewById(R.id.btn1);
        Button btn2=popupview.findViewById(R.id.btn2);
        final PopupWindow popupWindow=new PopupWindow
                (popupview, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.chunyu03));
        popupWindow.showAsDropDown(view,100,100);//显示在按钮下方有偏移
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("leo","点击了江西按钮");
                popupWindow.dismiss();//关闭
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("leo","点击了上海按钮");
                popupWindow.dismiss();//关闭
            }
        });
    }
}
